﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DacSanMienTayOBJ
{
    public class AdminBusiness
    {
        public static bool InsertAdmin(Admin admin)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                SqlParameter[] pa = new SqlParameter[4];
                string sql = "insert into admin(UserName,Password,Hashkey,IsVisible) values(@userName,@pass,@hashKey,@isVisible)";
                pa[0] = new SqlParameter("@userName", admin.UserName);
                pa[1] = new SqlParameter("@pass", admin.Password);
                pa[2] = new SqlParameter("@hashKey", admin.HashKey);
                pa[3] = new SqlParameter("@isVisible", admin.IsVisible);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception insert admin : " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool LoginManagerAccount(string username, string password)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                string sql = "select UserName,Password from admin where UserName=@userName and Password=@pass and IsVisible=1";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@userName", username);
                pa[1] = new SqlParameter("@pass", password);

                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception login admin : " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Admin> ListAllAdmin()
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {

                List<Admin> list = new List<Admin>();
                string sql = "select * from admin";
           
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                   Admin admin = new Admin();
                    admin.AdminID = int.Parse(reader["AdminID"].ToString());
                    admin.UserName = reader["UserName"].ToString();
                    admin.Password = reader["Password"].ToString();
                    admin.HashKey = reader["HashKey"].ToString();
                    admin.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    list.Add(admin);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception get list admind : " + ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static Admin GerAdminDetail(string username)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {

                Admin admin = null;
                string sql = "select * from admin where UserName=@userName";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@userName", username);

                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    admin = new Admin();
                    admin.AdminID = int.Parse(reader["AdminID"].ToString());
                    admin.UserName = reader["UserName"].ToString();
                    admin.Password = reader["Password"].ToString();
                    admin.HashKey = reader["HashKey"].ToString();
                    admin.IsVisible = int.Parse(reader["IsVisible"].ToString());
                }
                return admin;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception Get admindetail : " + ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static Admin GerAdminDetail(int adminID)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {

                Admin admin = null;
                string sql = "select * from admin where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@adminID",adminID);

                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    admin = new Admin();
                    admin.AdminID = int.Parse(reader["AdminID"].ToString());
                    admin.UserName = reader["UserName"].ToString();
                    admin.Password = reader["Password"].ToString();
                    admin.HashKey = reader["HashKey"].ToString();
                    admin.IsVisible = int.Parse(reader["IsVisible"].ToString());
                }
                return admin;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception get admindetail : " + ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static int GetManagerIDNewest()
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {

                string sql = "select AdminID from admin order by AdminID desc limit 1";
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader dr = command.ExecuteReader();
                int managerID = 0;
                if (dr.Read())
                {
                    managerID = int.Parse(dr["AdminID"].ToString());
                }
                return managerID;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception get managerid newest : " + ex.Message);
                return 0;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateStatusAdmin(int adminID, int visible)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                string sql = "update admin set IsVisible=@isVisible where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@adminID", adminID);
                pa[1] = new SqlParameter("@isVisible",visible);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                bool rs = false;
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception update status admin : " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateUserNameAndPasswordAdmin(int adminID, string userName, string password)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                string sql = "update admin set UserName=@userName,Password=@password where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[3];
                pa[0] = new SqlParameter("@adminID", adminID);
                pa[1] = new SqlParameter("@userName", userName);
                pa[2] = new SqlParameter("@password", password);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                bool rs = false;
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception update username and password admin : " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateUserNamedAdmin(int adminID, string userName)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                string sql = "update admin set UserName=@userName where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@adminID", adminID);
                pa[1] = new SqlParameter("@userName", userName);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                bool rs = false;
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog("Exception update username admin : " + ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool InsertAccessRight(AccessRight access)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                string sql = "insert into accessright(GroupID,AdminID) values(@groupID,@adminID)";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@adminID", access.AdminID);
                pa[1] = new SqlParameter("@groupID", access.GroupID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch
            {
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool DeleteAccessRight(int adminID)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                string sql = "delete from accessright where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@adminID", adminID);

                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch
            {
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<AccessRight> ListAccessRightByManagerID(int adminID)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                List<AccessRight> list = new List<AccessRight>();
                string sql = "select *from accessright where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@adminID", adminID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    AccessRight access = new AccessRight();
                    access.AdminID = int.Parse(reader["AdminID"].ToString());
                    access.GroupID = int.Parse(reader["GroupID"].ToString());
                    list.Add(access);
                }
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<GroupAdmin> ListGroupAdmin()
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                List<GroupAdmin> list = new List<GroupAdmin>();
                string sql = "select *from groupadmin";

                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    GroupAdmin group = new GroupAdmin();
                    group.GroupID = int.Parse(reader["GroupID"].ToString());
                    group.GroupName = reader["GroupName"].ToString();
                    group.Note = reader["Note"].ToString();
                    list.Add(group);
                }
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static GroupAdmin GetGroupAdminDetail(int groupID)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                GroupAdmin group = null;
                string sql = "select *from groupadmin where GroupID=@groupID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@groupID", groupID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    group = new GroupAdmin();
                    group.GroupID = int.Parse(reader["GroupID"].ToString());
                    group.GroupName = reader["GroupName"].ToString();
                    group.Note = reader["Note"].ToString();

                }
                return group;
            }
            catch
            {
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }
    }
}
